Script Modules - Custom UDP Processes

With Script Modules, instructors and students can now develop and add new protocols to PT. They are called custom processes and for now, PT only supports custom processes on top of UDP.

Create

The process can be created and started as follow:

    // create the custom process on the device
    var process = device.getProcess('UdpProcess').createCustomUdpProcess();

    // start the process listening on a specified UDP port
    process.start(1234);

Send Data

It can send data, but only supports a text payload right now.

    // send data to IP and UDP port
    process.sendData("text data", "1.1.1.1", 1234, null, null);

The last argument in sendData() is the outgoing port. If null, the lower processes, such as routing process on routers, would decide the outgoing port. Or it can be supplied:

    // send data to IP and UDP port and FastEthernet port
    var outPort = device.getPort("FastEthernet");
    process.sendData("some data", "1.1.1.1", 1234, null, outPort);

The second to last argument in sendData() is the frame instance. It is used in Simulation Mode to show the PDU color and details, such as what decisions are made on the PDU. There is currently no representation of the PDU format in PDU Details.

    // create a frame instance with color and destination
    var frameInstance = process.createFrameInstance(0xff0000, "1.1.1.1");

    // add a decision node so it shows in PDU Info
    if (frameInstance != null)
        frameInstance.addDecision("CHAT_SEND", "The chat process sends a message.", false, 7);

    process.sendData(data, dstIp, dstPort, frameInstance, null);

    // finalize the frame instance so it shows up in Simulation Mode
    process.finalizeFrameInstance(frameInstance);

Receive Data

When the custom UDP process receives a packet, it would use the delegate mechanism to have a Script Module do custom processing with the packet. First, the Script Module needs to register for the delegate, and then in the delegate function, process the data.

    // register for the processData delegate
    process.registerDelegate("processData", null, processData);

    processData = function(src, args)
    {
        doSomething(args.data, args.srcIp, args.srcPort);
    }